package com.cpecc.controller.wuzi;

import com.cpecc.constant.PositionEnum;
import com.cpecc.manager.system.DepManager;
import com.cpecc.manager.system.HuanJieManager;
import com.cpecc.manager.system.ToDoManager;
import com.cpecc.manager.system.UserManager;
import com.cpecc.manager.wuzi.BaoFeiJLManager;
import com.cpecc.manager.wuzi.BaoFeiManager;
import com.cpecc.manager.wuzi.BaoFeiSPRManager;
import com.cpecc.model.system.ToDo;
import com.cpecc.model.system.User;
import com.cpecc.model.wuzi.BaoFei;
import com.cpecc.model.wuzi.BaoFeiJL;
import com.cpecc.model.wuzi.BaoFeiSPR;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.wisteria.framework.web.APIResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/wuzi/baofeidan/")
public class BaoFeiDanController {

	@Autowired
	private BaoFeiManager baoFeiManager;

	@Autowired
	private HuanJieManager huanJieManager;

	@Autowired
	private ToDoManager toDoManager;

	@Autowired
	private UserManager userManager;

	@Autowired
	private BaoFeiSPRManager baoFeiSPRManager;

	@Autowired
	private DepManager depManager;

	@Autowired
	private BaoFeiJLManager baoFeiJLManager;

	/**
	 * 固资管理员发起填报报废列表
	 **/
	@RequestMapping(value="fqtbbfList")
	public ModelAndView fqtbbfList() {
		ModelAndView mav=new ModelAndView();
		mav.addObject("fqtbbfList", baoFeiManager.getBaoFeiList(715));
		mav.setViewName("baofei/baofeidan/fqtbbfListAjax");
		return mav;
	}

	/**
	 * 固资管理员发起填报报废表单
	 **/
	@RequestMapping(value="/fqtbbfForm")
	public ModelAndView faQiHuiQianForm(String id) {
		ModelAndView mav=new ModelAndView();
		mav.addObject("fqhqId", id);
		mav.addObject("depList", depManager.getAllDep());
		mav.setViewName("baofei/baofeidan/fqtbbfForm");
		return mav;
	}

	/**
	 * 固资管理员发起填报报废保存
	 * TODO 715
	 **/
	@RequestMapping(value="/fqtbbfSave")
	public void fqhqSave(String fqhqId, String shenPiRIds, HttpServletResponse response) {
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";

		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User currUser = userManager.findByUsername(name);

		boolean correct=true;
		if (shenPiRIds==null || shenPiRIds.length() < 1) {
			correct = false;
		}
		if (correct) {
			baoFeiSPRManager.deleteAllSPR(fqhqId);

			shenPiRIds=shenPiRIds.substring(0, shenPiRIds.length()-1);
			String shenPiRId[]=shenPiRIds.split(",");
			BaoFei baoFei=baoFeiManager.get(fqhqId);
			baoFei.setDangQianS(1);
			baoFei.setZongRS(shenPiRId.length);
			baoFei.setHuanJie(huanJieManager.get(717));
			baoFeiManager.cover(baoFei);

			for(int j=0;j<shenPiRId.length;j++){
				User user = userManager.getUser(shenPiRId[j]);
				BaoFeiSPR baoFeiSPR=new BaoFeiSPR();
				baoFeiSPR.setBaoFei(baoFei);
				baoFeiSPR.setUser(user);
				baoFeiSPR.setNo(0);
				baoFeiSPRManager.save(baoFeiSPR);
			}

			BaoFeiSPR baoFeiSPR = null;
			List<User> userList = userManager.getGKBMFZR();
			if (!CollectionUtils.isEmpty(userList)) {
				for (User u : userList) {
					baoFeiSPR = new BaoFeiSPR();
					baoFeiSPR.setNo(1);
					baoFeiSPR.setBaoFei(baoFei);
					baoFeiSPR.setUser(u);
					baoFeiSPRManager.save(baoFeiSPR);
				}
			}

			BaoFeiJL baoFeiJL=new BaoFeiJL();
			baoFeiJL.setBaoFei(baoFei);
			baoFeiJL.setCaoZuoRQ(new Date());
			baoFeiJL.setUser(currUser);
			baoFeiJL.setPiFuNR("固定资产管理员审批并发起报废");
			baoFeiJL.setPiFuLX("通过");
			baoFeiJL.setHuanJie(huanJieManager.get(715));
			baoFeiJLManager.save(baoFeiJL);
			toDoManager.deleteList(baoFei);
		} else {
			result="{\"result\":\"error\"}";
		}
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 报废使用人确认列表
	 * @return
	 */
	@RequestMapping(value = "/bfsyrqrList")
	public ModelAndView bfclqrList(){
		ModelAndView mav=new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		mav.addObject("bfsyrqrList", baoFeiManager.getBfsysqrList(user));
		mav.setViewName("baofei/baofeidan/bfsyrqrListAjax");
		return mav;
	}

	/**
	 * 报废使用人确认操作
	 * @param id
	 * @param response
	 */
	@RequestMapping(value = "/bfsyrqrSave")
	public void bfsyrqrSave(String id,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		BaoFei baoFei=baoFeiManager.get(id);
		baoFei.setHuanJie(huanJieManager.get(730));
		baoFeiManager.cover(baoFei);

		baoFeiSPRManager.deleteAllSPR(id);
		toDoManager.deleteList(baoFei);

		List<User> lingDaos = userManager.getBuMenLingDaos(user);
		if (lingDaos != null && lingDaos.size() > 0) {
			for (User u : lingDaos) {
				BaoFeiSPR baoFeiSPR=new BaoFeiSPR();
				baoFeiSPR.setBaoFei(baoFei);
				baoFeiSPR.setNo(1);
				baoFeiSPR.setUser(userManager.getUser(u.getId()));
				baoFei.setZongRS(baoFei.getZongRS()+1);
				baoFeiSPRManager.save(baoFeiSPR);
			}
		}

		BaoFeiJL baoFeiJL=new BaoFeiJL();
		baoFeiJL.setBaoFei(baoFei);
		baoFeiJL.setCaoZuoRQ(new Date());
		baoFeiJL.setUser(user);
		baoFeiJL.setPiFuNR("同意");
		baoFeiJL.setPiFuLX("通过");
		baoFeiJL.setHuanJie(huanJieManager.get(716));
		baoFeiJLManager.save(baoFeiJL);

		ToDo toDo=new ToDo();
		toDo.setBaoFei(baoFei);
		toDo.setCreateDate(new Date());
		toDo.setHuanJie(huanJieManager.get(716));
		toDo.setTask("报废使用人确认");
		toDo.setType("报废");
		toDo.setUser(baoFei.getCaiWuQRR());
		toDoManager.save(toDo);

		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "/gkbmPiSubmit")
	@ResponseBody
	public APIResult gkbmPiSubmit(HttpServletRequest request, HttpServletResponse response){
		String[] type = request.getParameterValues("bmbfId");
		if (type == null || type.length == 0) {
			return APIResult.fail("未选择审批内容");
		}

		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);

		if(type!=null){
			for(int i=0;i<type.length;i++){
				BaoFeiSPR baoFeiSPR=baoFeiSPRManager.get(type[i]);
				BaoFei baoFei=baoFeiSPR.getBaoFei();

				baoFeiSPRManager.deleteAllSPR(baoFei.getId());
				toDoManager.delete(baoFei, user);

				baoFei.setDangQianS(baoFei.getDangQianS()+1);
				baoFei.setHuanJie(huanJieManager.get(718));
				baoFei.setZongRS(0);
				baoFeiManager.cover(baoFei);

				baoFeiSPRManager.updateSPRNO(baoFei.getId());

				BaoFeiJL baoFeiJL=new BaoFeiJL();
				baoFeiJL.setBaoFei(baoFei);
				baoFeiJL.setCaoZuoRQ(new Date());
				baoFeiJL.setHuanJie(huanJieManager.get(717));
				baoFeiJL.setPiFuLX("通过");
				baoFeiJL.setPiFuNR("同意");
				baoFeiJL.setUser(user);
				baoFeiJLManager.save(baoFeiJL);
			}
		}
		return APIResult.success();
	}

	/**
	 * 报废归口部门领导审批同意操作
	 * @param id
	 * @param response
	 */
	@RequestMapping(value = "/gkbmsptySave")
	public void gkbmsptySave(String id, HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);

		BaoFeiSPR baoFeiSPR=baoFeiSPRManager.get(id);
		BaoFei baoFei=baoFeiSPR.getBaoFei();

		baoFeiSPRManager.deleteAllSPR(baoFei.getId());
		toDoManager.delete(baoFei, user);

		baoFei.setDangQianS(baoFei.getDangQianS()+1);
		baoFei.setHuanJie(huanJieManager.get(718));
		baoFei.setZongRS(0);
		baoFeiManager.cover(baoFei);

		baoFeiSPRManager.updateSPRNO(baoFei.getId());

		BaoFeiJL baoFeiJL=new BaoFeiJL();
		baoFeiJL.setBaoFei(baoFei);
		baoFeiJL.setCaoZuoRQ(new Date());
		baoFeiJL.setHuanJie(huanJieManager.get(717));
		baoFeiJL.setPiFuLX("通过");
		baoFeiJL.setPiFuNR("同意");
		baoFeiJL.setUser(user);
		baoFeiJLManager.save(baoFeiJL);

		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 报废部门领导审批不同意操作
	 * @param id
	 * @param response
	 */
	@RequestMapping(value = "/bfbmldspbtySave")
	public void bfbmldspbtySave(String id, HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);

		BaoFeiSPR baoFeiSPR=baoFeiSPRManager.get(id);
		BaoFei baoFei=baoFeiSPR.getBaoFei();
		baoFei.setDangQianS(baoFei.getDangQianS()+1);

		baoFei.setHuanJie(huanJieManager.get(716));
		baoFei.setZongRS(0);
		baoFeiManager.cover(baoFei);

		baoFeiSPRManager.delete(id);

		BaoFeiSPR syrQR = new BaoFeiSPR();
		syrQR.setBaoFei(baoFei);
		syrQR.setUser(baoFei.getWuZi().getUser());
		syrQR.setNo(1);
		baoFeiSPRManager.save(syrQR);

		toDoManager.delete(baoFei, user);

		BaoFeiJL baoFeiJL=new BaoFeiJL();
		baoFeiJL.setBaoFei(baoFei);
		baoFeiJL.setCaoZuoRQ(new Date());
		baoFeiJL.setHuanJie(huanJieManager.get(730));
		baoFeiJL.setPiFuLX("不通过");
		baoFeiJL.setPiFuNR("不同意");
		baoFeiJL.setUser(user);
		baoFeiJLManager.save(baoFeiJL);

		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 相关部门审批列表
	 * @return
	 */
	@RequestMapping(value = "/bfxgbmspList")
	public ModelAndView bfxgbmspList(){
		ModelAndView mav=new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		mav.addObject("bfxgbmspList", baoFeiSPRManager.getBfxgbmspList(user));
		mav.setViewName("baofei/baofeidan/bfxgbmspListAjax");
		return mav;
	}

	/**
	 * 相关部门审批同意操作
	 * @param id		报废物质ID
	 * @param response	response
	 */
	@RequestMapping(value = "/bfxgbmsptySave")
	public void bfxgbmsptySave(String id,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);

		BaoFeiSPR bfSPR = baoFeiSPRManager.get(id);
		BaoFei baoFei=bfSPR.getBaoFei();

		baoFeiSPRManager.delete(id);
		toDoManager.deleteList(baoFei);

		if (baoFeiSPRManager.isOver(baoFei.getId())) {
			baoFei.setHuanJie(huanJieManager.get(711));
			baoFeiManager.cover(baoFei);

			List<User> userList = userManager.getGZGLY();
			BaoFeiSPR baoFeiSPR = null;
			for (User u : userList) {
				baoFeiSPR = new BaoFeiSPR();
				baoFeiSPR.setBaoFei(baoFei);
				baoFeiSPR.setNo(1);
				baoFeiSPR.setUser(u);
				baoFeiSPRManager.save(baoFeiSPR);
			}
		}

		// 记录操作日志
		BaoFeiJL baoFeiJL=new BaoFeiJL();
		baoFeiJL.setBaoFei(baoFei);
		baoFeiJL.setCaoZuoRQ(new Date());
		baoFeiJL.setUser(user);
		baoFeiJL.setPiFuNR("同意");
		baoFeiJL.setPiFuLX("通过");
		baoFeiJL.setHuanJie(huanJieManager.get(718));
		baoFeiJLManager.save(baoFeiJL);

		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 财务审批确认操作
	 * @param id		报废物质ID
	 * @param response	response
	 */
	@RequestMapping(value = "/cwspqrSave")
	public void bfclrzSave(String id,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);

		BaoFei baoFei=baoFeiManager.get(id);
		int currenthuanJieId = baoFei.getHuanJie().getId();
		int nextHuanJieId = currenthuanJieId;
		if (currenthuanJieId == 719) {
			nextHuanJieId = 720;
		} else if (currenthuanJieId == 726) {
			nextHuanJieId = 713;
		}

		if (currenthuanJieId != nextHuanJieId) {
			baoFei.setHuanJie(huanJieManager.get(nextHuanJieId));
			baoFeiManager.cover(baoFei);

			baoFeiSPRManager.deleteAllSPR(id);
			toDoManager.deleteList(baoFei);

			if (nextHuanJieId != 713) {
				// 指定下个环节部门分管领导用户
				BaoFeiSPR baoFeiSPR = new BaoFeiSPR();
				baoFeiSPR.setBaoFei(baoFei);
				baoFeiSPR.setNo(1);
				baoFeiSPR.setUser(userManager.getBuMenFenGuanLingDao(baoFei.getWuZi().getDep().getId()));
				baoFeiSPRManager.save(baoFeiSPR);
			}

			BaoFeiJL baoFeiJL=new BaoFeiJL();
			baoFeiJL.setBaoFei(baoFei);
			baoFeiJL.setCaoZuoRQ(new Date());
			baoFeiJL.setUser(user);
			baoFeiJL.setPiFuNR("同意");
			baoFeiJL.setPiFuLX("通过");
			baoFeiJL.setHuanJie(huanJieManager.get(currenthuanJieId));
			baoFeiJLManager.save(baoFeiJL);
		}

		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 分管领导审批列表
	 * @return
	 */
	@RequestMapping(value = "/bffgldspList")
	public ModelAndView bffgldtyList(){
		ModelAndView mav=new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		mav.addObject("bffgldspList", baoFeiSPRManager.getBaoFeiList(user, 720));
		mav.setViewName("baofei/baofeidan/bffgldspListAjax");
		return mav;
	}

	/**
	 * 分管领导审批同意操作
	 * @param id		报废物质ID
	 * @param response	response
	 */
	@RequestMapping(value = "/bffgldsptySave")
	public void fgldtySave(String id,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);

		BaoFeiSPR bfSRP = baoFeiSPRManager.get(id);
		BaoFei baoFei=bfSRP.getBaoFei();
		baoFei.setHuanJie(huanJieManager.get(721));
		baoFeiManager.cover(baoFei);

		baoFeiSPRManager.deleteAllSPR(baoFei.getId());
		toDoManager.deleteList(baoFei);

		// 指定下个环节总经理审批用户
		List<User> list = userManager.getUsersByGangWei(PositionEnum.ZongJingLi.getValue());
		if (!CollectionUtils.isEmpty(list)) {
			BaoFeiSPR baoFeiSPR = new BaoFeiSPR();
			baoFeiSPR.setBaoFei(baoFei);
			baoFeiSPR.setNo(1);
			baoFeiSPR.setUser(list.get(0));
			baoFeiSPRManager.save(baoFeiSPR);
		}


		BaoFeiJL baoFeiJL=new BaoFeiJL();
		baoFeiJL.setBaoFei(baoFei);
		baoFeiJL.setCaoZuoRQ(new Date());
		baoFeiJL.setUser(user);
		baoFeiJL.setPiFuNR("同意");
		baoFeiJL.setPiFuLX("通过");
		baoFeiJL.setHuanJie(huanJieManager.get(720));
		baoFeiJLManager.save(baoFeiJL);

		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 总经理审批列表
	 * @return
	 */
	@RequestMapping(value = "/bfzjlspList")
	public ModelAndView bfzjlspList(){
		ModelAndView mav=new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		mav.addObject("bfzjlspList", baoFeiSPRManager.getBaoFeiList(user, 721));
		mav.setViewName("baofei/baofeidan/bfzjlspListAjax");
		return mav;
	}

	/**
	 * 总经理审批同意操作
	 * @param id		报废物质ID
	 * @param response	response
	 */
	@RequestMapping(value = "/bfzjlsptySave")
	public void bfzjlsptySave(String id,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);

		BaoFei baoFei=baoFeiManager.get(id);
		baoFei.setHuanJie(huanJieManager.get(722));
		baoFeiManager.cover(baoFei);

		baoFeiSPRManager.deleteAllSPR(id);
		toDoManager.deleteList(baoFei);

		// 指定下个环节董事长审批用户
		List<User> list = userManager.getUsersByGangWei(PositionEnum.DongShiZhang.getValue());
		if (!CollectionUtils.isEmpty(list)) {
			BaoFeiSPR baoFeiSPR = new BaoFeiSPR();
			baoFeiSPR.setBaoFei(baoFei);
			baoFeiSPR.setNo(1);
			baoFeiSPR.setUser(list.get(0));
			baoFeiSPRManager.save(baoFeiSPR);
		}


		BaoFeiJL baoFeiJL=new BaoFeiJL();
		baoFeiJL.setBaoFei(baoFei);
		baoFeiJL.setCaoZuoRQ(new Date());
		baoFeiJL.setUser(user);
		baoFeiJL.setPiFuNR("同意");
		baoFeiJL.setPiFuLX("通过");
		baoFeiJL.setHuanJie(huanJieManager.get(721));
		baoFeiJLManager.save(baoFeiJL);

		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 董事长审批列表
	 * @return
	 */
	@RequestMapping(value = "/bfdszspList")
	public ModelAndView bfdszspList(){
		ModelAndView mav=new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		mav.addObject("bfdszspList", baoFeiSPRManager.getBaoFeiList(user, 722));
		mav.setViewName("baofei/baofeidan/bfdszspListAjax");
		return mav;
	}

	/**
	 * 董事长审批同意操作
	 * @param id		报废物质ID
	 * @param response	response
	 */
	@RequestMapping(value = "/bfdszsptySave")
	public void dszsptySave(String id,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);

		BaoFeiSPR bfSPR = baoFeiSPRManager.get(id);
		BaoFei baoFei=bfSPR.getBaoFei();
		baoFei.setHuanJie(huanJieManager.get(723));
		baoFeiManager.cover(baoFei);

		baoFeiSPRManager.delete(id);
		toDoManager.deleteList(baoFei);

		BaoFeiJL baoFeiJL=new BaoFeiJL();
		baoFeiJL.setBaoFei(baoFei);
		baoFeiJL.setCaoZuoRQ(new Date());
		baoFeiJL.setUser(user);
		baoFeiJL.setPiFuNR("同意");
		baoFeiJL.setPiFuLX("通过");
		baoFeiJL.setHuanJie(huanJieManager.get(722));
		baoFeiJLManager.save(baoFeiJL);

		List<User> userList = userManager.getGZGLY();
		if (!CollectionUtils.isEmpty(userList)) {
			for (User u : userList) {
				BaoFeiSPR baoFeiSPR = new BaoFeiSPR();
				baoFeiSPR.setUser(u);
				baoFeiSPR.setBaoFei(baoFei);
				baoFeiSPR.setNo(1);
				baoFeiSPRManager.save(baoFeiSPR);
			}
		}

		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "/bfsbbmldSave")
	public void bfsbbmldSave(BaoFei temp, HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		String id= temp.getId();

		BaoFei baoFei=baoFeiManager.get(id);
		baoFei.setHuanJie(huanJieManager.get(724));
		baoFeiManager.cover(baoFei);

		baoFeiSPRManager.deleteAllSPR(id);
		toDoManager.deleteList(baoFei);

		List<User> lingDaos = userManager.getBuMenLingDaos(user);
		if (lingDaos != null && lingDaos.size() > 0) {
			for (User u : lingDaos) {
				BaoFeiSPR baoFeiSPR=new BaoFeiSPR();
				baoFeiSPR.setBaoFei(baoFei);
				baoFeiSPR.setNo(1);
				baoFeiSPR.setUser(userManager.getUser(u.getId()));
				baoFei.setZongRS(baoFei.getZongRS()+1);
				baoFeiSPRManager.save(baoFeiSPR);
			}
		}

		BaoFeiJL baoFeiJL=new BaoFeiJL();
		baoFeiJL.setBaoFei(baoFei);
		baoFeiJL.setCaoZuoRQ(new Date());
		baoFeiJL.setUser(user);
		baoFeiJL.setPiFuNR("确认");
		baoFeiJL.setPiFuLX("通过");
		baoFeiJL.setHuanJie(huanJieManager.get(723));
		baoFeiJLManager.save(baoFeiJL);

		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 报废上报部门领导审批同意操作
	 * @param id
	 * @param response
	 */
	@RequestMapping(value = "/bfsbbmldsptySave")
	public void bfsbbmldsptySave(String id, HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);

		BaoFeiSPR baoFeiSPR=baoFeiSPRManager.get(id);
		BaoFei baoFei=baoFeiSPR.getBaoFei();

		baoFeiSPRManager.deleteAllSPR(baoFei.getId());
		toDoManager.delete(baoFei, user);

		baoFei.setDangQianS(baoFei.getDangQianS()+1);
		baoFei.setHuanJie(huanJieManager.get(725));
		baoFei.setZongRS(0);
		baoFeiManager.cover(baoFei);

		List<User> userList = userManager.getGZGLY();
		if (!CollectionUtils.isEmpty(userList)) {
			for (User u : userList) {
				BaoFeiSPR baoFeiSPR1 = new BaoFeiSPR();
				baoFeiSPR1.setUser(u);
				baoFeiSPR1.setBaoFei(baoFei);
				baoFeiSPR1.setNo(1);
				baoFeiSPRManager.save(baoFeiSPR1);
			}
		}

		BaoFeiJL baoFeiJL=new BaoFeiJL();
		baoFeiJL.setBaoFei(baoFei);
		baoFeiJL.setCaoZuoRQ(new Date());
		baoFeiJL.setHuanJie(huanJieManager.get(724));
		baoFeiJL.setPiFuLX("通过");
		baoFeiJL.setPiFuNR("同意");
		baoFeiJL.setUser(user);
		baoFeiJLManager.save(baoFeiJL);

		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "/bftxclqkSave")
	public void bftxclqkSave(BaoFei temp,String caiWuRId,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		BaoFei baoFei=baoFeiManager.get(temp.getId());

		toDoManager.deleteList(baoFei);
		baoFeiSPRManager.deleteAllSPR(baoFei.getId());

		baoFei.setChuLiQK(temp.getChuLiQK());
		baoFei.setHuiShouJE(temp.getHuiShouJE());
		baoFei.setWanChengRQ(new Date());
		baoFei.setChuLiFS(temp.getChuLiFS());
		baoFei.setCaiWuQRR(userManager.getUser(caiWuRId));
		baoFei.setHuanJie(huanJieManager.get(726));
		baoFeiManager.cover(baoFei);

		BaoFeiJL baoFeiJL=new BaoFeiJL();
		baoFeiJL.setBaoFei(baoFei);
		baoFeiJL.setCaoZuoRQ(new Date());
		baoFeiJL.setHuanJie(huanJieManager.get(725));
		baoFeiJL.setPiFuLX("填写报废处理情况");
		baoFeiJL.setPiFuNR("已填");
		baoFeiJL.setUser(user);
		baoFeiJLManager.save(baoFeiJL);

		response.setContentType("application/json");
		try {
			String result="{\"result\":\"success\"}";
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
